package at.horario.web.pages;

import at.horario.model.User;
import at.horario.web.HorarioSession;
import at.horario.web.component.FeedbackViewPanel;
import at.horario.web.component.MenuPanel;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.IPageMap;
import org.apache.wicket.PageParameters;
import org.apache.wicket.ResourceReference;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.ResourceModel;

public abstract class PageTemplate extends WebPage {
    transient protected Log log = LogFactory.getLog(getClass());
    protected FeedbackViewPanel feedback;


    private void init() {
        final String className = getClass().getSimpleName().toLowerCase();
        add(new Label("pageTitle", getLocalizer().getString(className + ".title", this) + " | " + getLocalizer().getString("webapp.name", this)));
        add(new Label("heading", new ResourceModel(className + ".heading")));
        final ResourceReference theme = new ResourceReference("styles/andreas01/theme.css");
//        add(new StyleSheetReference("themeCss", theme));
//        add(new StyleSheetReference("printCss", new ResourceReference("styles/andreas01/print.css")));


        MenuPanel menuPanel = new MenuPanel("menuBar");

        menuPanel.addMenu(Index.class);
        if (!getSession().isSignedIn())
            menuPanel.addMenu(Login.class);
        menuPanel.addMenu(UserList.class);
        menuPanel.addMenu(UserForm.class);
        menuPanel.addMenu(HorarioForm.class);
        menuPanel.addMenu(HorarioList.class);
        menuPanel.addMenu(HorarioScheduleList.class);
        if (getSession().isSignedIn())
            menuPanel.addMenu(Logout.class);

        add(menuPanel);


        feedback = new FeedbackViewPanel("feedback");
        feedback.setOutputMarkupPlaceholderTag(true);
        add(feedback);
//        IndicatingAjaxLink indicatingAjaxLink = new IndicatingAjaxLink("indicatingAjaxLink", new Model("link")) {
//            public void onClick(AjaxRequestTarget target) {
//                System.out.println(target);
//            }
//        };
//        add(indicatingAjaxLink);

        Label labelEntrou;
        User user = ((HorarioSession) getSession()).getUser();
        if (user != null) {
            String entrou = "Entrou como " + user.getUsername();
            labelEntrou = new Label("entrou", new Model(entrou));
            labelEntrou.setVisible(true);
        } else {
            String entrou = "";
            labelEntrou = new Label("entrou", new Model(entrou));
            labelEntrou.setVisible(false);
        }
        add(labelEntrou);
    }


    public PageTemplate() {
        init();
    }

    public PageTemplate(PageParameters parameters) {
        super(parameters);
        init();
    }

    public PageTemplate(IModel model) {
        super(model);
        init();
    }

    public PageTemplate(IPageMap pageMap) {
        super(pageMap);
        init();
    }

    public PageTemplate(IPageMap pageMap, IModel model) {
        super(pageMap, model);
        init();
    }


    public HorarioSession getSession() {
        return (HorarioSession) super.getSession();
    }


}
